import json
import time
from urllib import request

import mysql.connector
from fdfs_client.client import get_tracker_conf, Fdfs_client


def initMysql():
  global mydb
  global mycursor

  # mydb = mysql.connector.connect(
  #   host='8.129.219.66',
  #   user='guifangtong',
  #   passwd='guifangtong1234!@#',
  #   database='guifangtong'
  # )

  mydb = mysql.connector.connect(
    host='localhost',
    user='root',
    passwd='root1234',
    database='guifangtong'
  )

  mycursor = mydb.cursor()


def sousuoLgb():
  global page_index
  page_index = 3
  url = 'https://hfb.hengfangbao.cn/api/public/news?cate_id=0&page=' + str(page_index)
  print("访问", url)
  # 直接请求
  response = request.urlopen(url)
  # 获取状态码，如果是200表示成功
  # print(response.status)
  # 读取网页内容
  context = response.read().decode('utf-8')
  data = json.loads(context)
  result = data['result']
  items = result['items']
  for item in items:
    newXq(str(item['item_id']), item['thumb'])
    time.sleep(1)


def uploadPic(picurl):
  try:
    tracker_path = get_tracker_conf('./client.conf')
    client = Fdfs_client(tracker_path)
    # ret_upload
    # ret_upload = client.upload_by_filename('D://12.jpg') # 图片地址
    resp = request.urlopen(picurl)
    # image = np.asarray(bytearray(resp.read()), dtype="uint8")
    ret_upload = client.upload_by_buffer(resp.read())  # 图片地址
    # print(ret_upload['Remote file_id'])
    # print(ret_upload)
    return ret_upload['Remote file_id']
  except Exception as e:
    print('连接失败', e)


def picQw(thumb):
  response = request.urlopen(thumb)
  if response.status == 200:
    return 1
  else:
    return 0
  # 获取状态码，如果是200表示成功
  # print(response.status)


def addNews(newid, thumb, news):
  initMysql()
  table_name = 't_news'
  try:
    pic = uploadPic(thumb)
    picurl = pic.decode('utf-8')
  except:
    picurl = "group1/M00/00/16/rBOXs1-_rRSADGRUAAP8z0FXjvM6775917"

  print('图片', picurl)

  form_x = news['from']
  if form_x == '':
    form_x = '桂房通'

  details = news['content']
  detailss = ''
  for s in details:
    if s == '"':
      s = "'"
    detailss += s
  sql = "INSERT INTO " + table_name + "(title,create_time,key_word,author,pic,content,date) VALUES "
  val = '("' + news['title'] + '","' + news['created_at'] + '","' + form_x + '","' + news[
    'author'] + '","' + picurl + '","' + detailss + '", "' + newid + '");'
  sqlStr = sql + val
  # print('sqlStr', sqlStr)
  print('原图', thumb)
  f = "./sql/news_index_" + str(page_index) + "_hengfangbao.sql"
  with open(f, "a", encoding="utf-8") as file:
    file.write(sqlStr + '\n')
    file.close()
# try:
#   mycursor.execute(sql, val)
#   mydb.commit()
#   print('成功了')
# except Exception as e:
#   print('出错了',e)

def newXq(newid, thumb):
  print('新闻详情', newid)
  xq_url = 'https://hfb.hengfangbao.cn/api/public/news_detail?item_id=' + str(newid)
  response = request.urlopen(xq_url)
  # 获取状态码，如果是200表示成功
  # print(response.status)
  # 读取网页内容
  context = response.read().decode('utf-8')
  data = json.loads(context)
  news = data['result']
  addNews(newid, thumb, news)


if __name__ == '__main__':
  sousuoLgb()
